《
<template>
  <div>
    <h1>活动列表</h1>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="活动名称">
          <el-input
            v-model="searchForm.activeName"
            placeholder="请输入活动名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadList">查询</el-button>
          <el-button type="primary" @click="toaddActive">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="acitiveId" label="活动ID" />
        <el-table-column prop="activeName" label="活动名称" />
        <el-table-column prop="groupNum" label="组团人数" />
        <el-table-column prop="beginTime" label="开始时间" />
        <el-table-column prop="endTime" label="截止时间" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              type="primary"
              :icon="Edit"
              circle
              @click="toupdateActive(scope.row)"
            />

            <el-button
              type="danger"
              :icon="Delete"
              circle
              @click="toDeleteActive(scope.row)"
            />

            <el-button type="primary" @click="goActive(scope.row)"
              >参与拼团</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :size="size"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加活动" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="活动名称"
            :label-width="formLabelWidth"
            prop="activeName"
          >
            <el-input v-model="form.activeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="组团人数"
            :label-width="formLabelWidth"
            prop="groupNum"
          >
            <el-input-number v-model="form.groupNum" />
          </el-form-item>
          <el-form-item label="开始日期" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.beginTime"
              type="datetime"
              placeholder="请选择一个时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="结束时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请选择一个结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div>
      <el-dialog v-model="dialogFormVisibless" title="修改活动" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="活动名称"
            :label-width="formLabelWidth"
            prop="activeName"
          >
            <el-input v-model="form.activeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="组团人数"
            :label-width="formLabelWidth"
            prop="groupNum"
          >
            <el-input-number v-model="form.groupNum" />
          </el-form-item>
          <el-form-item label="开始日期" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.beginTime"
              type="datetime"
              placeholder="请选择一个时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="结束时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请选择一个结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="updateSubmitForm(ruleFormRef)">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div>
      <el-dialog v-model="dialogFormVisiblesess" title="参与拼团" width="500">
        <el-form :model="joinForm">
          <el-form-item label="拼团活动名称" :label-width="formLabelWidth">
            <el-input v-model="joinForm.joinName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="活动简介" :label-width="formLabelWidth">
            <el-input v-model="joinForm.joinTip" autocomplete="off" />
          </el-form-item>
          <el-form-item label="商品单位" :label-width="formLabelWidth">
            <el-select
              v-model="joinForm.prodUnit"
              placeholder="请选择"
              style="width: 240px"
            >
              <el-option
                v-for="item in unitList"
                :key="item.dictId"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="商品轮播图" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:10010/prod/type/uploadImages"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>

          <el-form-item label="活动开始时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="joinForm.beginTime"
              type="datetime"
              placeholder="请选择一个开始时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="活动结束时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="joinForm.endTime"
              type="datetime"
              placeholder="请选择一个结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="拼团人数" :label-width="formLabelWidth">
            <el-input-number v-model="joinForm.groupNum" />
          </el-form-item>
          <el-form-item label="购买数量" :label-width="formLabelWidth">
            <el-input-number v-model="joinForm.buyMax" />
          </el-form-item>
          <el-form-item label="补齐人数" :label-width="formLabelWidth">
            <el-input-number v-model="joinForm.virtualNum" />
          </el-form-item>
          <el-form-item label="拼团商品" :label-width="formLabelWidth">
            <el-select
              v-model="joinForm.prodInfoId"
              placeholder="请选择"
              style="width: 240px"
            >
              <el-option
                v-for="item in prodInfoList"
                :label="item.prodName"
                :value="item.infoId"
                :key="item.infoId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="配送方式" :label-width="formLabelWidth">
            <el-select
              v-model="joinForm.sendKind"
              placeholder="请选择"
              style="width: 240px"
            >
              <el-option
                v-for="item in sendList"
                :key="item.dictId"
                :label="item.dictName"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="运费" :label-width="formLabelWidth">
            <el-input v-model="joinForm.sendMoney" autocomplete="off" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="addInfoSubmitForm">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";

import { type ComponentSize } from "element-plus";
import {
  activeList,
  addActive,
  updateActive,
  deleteActive,
  addJoinList,
} from "../api/active";
import { getDictList, getProdInfoList } from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import type {
  FormInstance,
  FormRules,
  UploadProps,
  UploadUserFile,
} from "element-plus";
import {
  Check,
  Delete,
  Edit,
  Message,
  Search,
  Star,
} from "@element-plus/icons-vue";
interface RuleForm {
  activeName: string;
  groupNum: string;
}
const ruleFormRef = ref<FormInstance>();

const rules = reactive<FormRules<RuleForm>>({
  activeName: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
  groupNum: [{ required: true, message: "请输入入团人数", trigger: "blur" }],
});
const tableData = ref([]);
const searchForm = ref({
  activeName: null,
  pageNum: 1,
  pageSize: 2,
});
const total = ref(0);

import { Plus } from "@element-plus/icons-vue";

const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
};

const dialogFormVisible = ref(false);
const dialogFormVisibless = ref(false);
const dialogFormVisiblesess = ref(false);
const formLabelWidth = "140px";

const form = ref({
  acitiveId: 0,
  activeName: null,
  beginTime: "",
  createdBy: "",
  createdTime: "",
  endTime: "",
  groupNum: "",
});
const joinForm = ref({
  beginTime: "",
  buyMax: "",
  createdBy: "",
  createdTime: "",
  endTime: "",
  expireTime: "",
  groupNum: "",
  joinDesc: "",
  joinId: 0,
  joinImages: "",
  joinName: null,
  joinTip: null,
  joinTop: "",
  prodInfoId: "",
  prodUnit: "",
  sendKind: "",
  sendMoney: "",
  sendPay: "",
  virtualNum: "",
});

const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};

const loadList = () => {
  activeList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

//去往活动添加页面
const toaddActive = () => {
  form.value = {
    acitiveId: 0,
    activeName: null,
    beginTime: "",
    createdBy: "",
    createdTime: "",
    endTime: "",
    groupNum: "",
  };
  dialogFormVisible.value = true;
};
//添加活动
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      addActive(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

//去往修改页面
const toupdateActive = (row: any) => {
  Object.assign(form.value, row);
  dialogFormVisibless.value = true;
};

//修改活动
const updateSubmitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      updateActive(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("修改成功");
          dialogFormVisibless.value = false;
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

//删除
const toDeleteActive = (row: any) => {
  ElMessageBox.confirm("删除后无法恢复，确认删除吗?", "等待", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteActive(row.acitiveId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadList();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};

//参与拼团
const goActive = (row: any) => {
  //清空表
  joinForm.value = {
    beginTime: "",
    buyMax: "",
    createdBy: "",
    createdTime: "",
    endTime: "",
    expireTime: "",
    groupNum: "",
    joinDesc: "",
    joinId: 0,
    joinImages: "",
    joinName: null,
    joinTip: null,
    joinTop: "",
    prodInfoId: "",
    prodUnit: "",
    sendKind: "",
    sendMoney: "",
    sendPay: "",
    virtualNum: "",
  };
  dialogFormVisiblesess.value = true;
};

const addInfoSubmitForm = () => {
  joinForm.value.joinImages = imageUrl.value;
  addJoinList(joinForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("参与成功");
      dialogFormVisiblesess.value = false;
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const unitList = ref([]);
const prodInfoList = ref([]);
const sendList = ref([]);

onMounted(() => {
  loadList();
  //单位列表
  getDictList(2).then((res) => {
    unitList.value = res.data.data;
  });
  //配送方式列表
  getDictList(16).then((res) => {
    sendList.value = res.data.data;
  });
  //获取商品信息列表
  getProdInfoList().then((res) => {
    if (res.data.code == 200) {
      prodInfoList.value = res.data.data;
    }
  });
});
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
